package com.jy.novel.service.system;

import com.jy.novel.common.Page;
import com.jy.novel.dao.system.UserDao;
import com.jy.novel.pojo.form.UserEditForm;
import com.jy.novel.pojo.form.UserQueryForm;
import com.jy.novel.pojo.system.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;


/**
 * Created by yangqy on 4/29/16.
 */
@Service
public class UserService {

    @Autowired
    UserDao userDao;

    public Page<UserInfo> findUserPage(UserQueryForm form, int curPage, int pageSize) {
        return this.userDao.findUserPage(form,curPage, pageSize);
    }

    public UserInfo getUserInfo(String loginName) {
        return this.userDao.getUserInfo(loginName);
    }

    public boolean addUserInfo(UserInfo userInfo) {
        userInfo.setLoginPass(DigestUtils.md5Hex(userInfo.getLoginPass()));
        return this.userDao.addUserInfo(userInfo) > 0;
    }

    public boolean changeUserIsLock(String loginName,Integer isLock) {
        return this.userDao.changeUserIsLock(loginName, isLock) > 0;
    }


    public boolean modifyUserPwd(String loginName, String pwd) {
        pwd = DigestUtils.md5Hex(pwd);
        return this.userDao.modifyUserPwd(loginName, pwd) > 0;
    }

    public boolean updateLoginTime(String loginName) {
        return this.userDao.updateLoginTime(loginName) > 0;
    }

    public boolean existUserInfo(String loginName) {
        return this.userDao.existUserInfo(loginName);
    }

    public boolean updateUserInfo(UserEditForm form) {
        if (form.getLoginName()==null){
            return false;
        }
        return this.userDao.updateUserInfo(form) > 0;
    }

    public int deleteUser(String loginName) {
        return this.userDao.deleteUser(loginName);
    }
}
